package com.sk.waste.app.views;

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.WindowManager
import android.webkit.WebView
import android.widget.Button
import android.widget.LinearLayout.LayoutParams
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lc.smc.lib.interfaces.LCInterface
import com.lc.smc.lib.utils.LCBaseUtil
import com.lc.smc.lib.utils.LCSPUtil
import com.lc.smc.lib.utils.LCScreenUtil
import com.sk.waste.app.R
import com.sk.waste.app.adapters.DistrictAdapter
import com.sk.waste.app.config.AppConfig
import com.sk.waste.app.config.NetWorkConfig
import com.sk.waste.app.models.DistrictModel
import com.sk.waste.app.models.TypeModel
import com.sk.waste.app.utils.OkHttpUtil
import com.sk.waste.app.views.webview.LCWebView
import okhttp3.Call
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException


/**
 *  @time 2025/5/14  9:07
 *  @desc 隐私政策弹窗
 */
class ProvacyPopuWindow() : PopupWindow() {

    private val TAG = "LCLOG_FeaturePopuWindow"
    private lateinit var mContext: Activity
    private lateinit var mMenuView: View
    private lateinit var mWebView: WebView
    private lateinit var bttCancle: TextView
    private lateinit var bttSure: TextView
    private lateinit var callBackListener: LCInterface.LcCallBack

    fun init(context: Activity) {
        this.mContext = context
        findView()
        initPopu()
    }

    //初始化
    private fun initPopu() {
        this.contentView = mMenuView
        this.width = LCScreenUtil.getWidth(mContext)-150
//        this.height = LCScreenUtil.getHeight(mContext)/3*2
        this.height = LayoutParams.WRAP_CONTENT
        //设置SelectPicPopupWindow弹出窗体可点击
        this.isFocusable = false
        this.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
        this.isOutsideTouchable = false
        this.isTouchable = true
        this.isClippingEnabled = false ///覆盖状态栏，
        setTouchInterceptor { view, motionEvent -> false }
    }

    @SuppressLint("NewApi")
    private fun findView() {
        val inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        mMenuView = inflater.inflate(R.layout.view_provacy_popuwindow, null)
        mWebView = mMenuView.findViewById(R.id.view_provacy_webview)
        bttCancle = mMenuView.findViewById(R.id.view_provacy_cancle)
        bttSure = mMenuView.findViewById(R.id.view_provacy_sure)
        mWebView.loadUrl("http://waste.biddingcloud.cn/privacy_agreement_in.html")
        bttCancle.setOnClickListener {
            callBackListener.callBack(false)
            dismiss()
        }
        bttSure.setOnClickListener {
            callBackListener.callBack(true)
            dismiss()
        }
    }

    override fun showAtLocation(parent: View?, gravity: Int, x: Int, y: Int) {
        setBackgroundAlpha(0.8f)
        super.showAtLocation(parent, gravity, x, y)
    }

    /**
     *  @time 2024/7/8  16:39
     *  @author Im_jingwei
     *  @desc 事件回调接口
     */
    fun setPopuOnclickListener(listener: LCInterface.LcCallBack) {
        this.callBackListener = listener
    }

    /**
     *  @time 2024/7/8  16:39
     *  @author Im_jingwei
     *  @desc 销毁
     */
    override fun dismiss() {
        //关闭软键盘
        setBackgroundAlpha(1.0f)
        super.dismiss()
    }

    //设置窗口颜色
    private fun setBackgroundAlpha(bgAlpha: Float) {
        val lp = (mContext as Activity).window.attributes
        (mContext as Activity).window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        lp.alpha = bgAlpha //0.0-1.0
        (mContext as Activity).window.attributes = lp
    }

}